package com.zrrd.map;

import java.util.TreeMap;
import java.util.function.BiConsumer;

/**
 * @author WanJl
 * @version 1.0
 * @title TreeMapTest01
 * @description
 * @create 2024/4/21
 */
public class TreeMapTest01 {
    public static void main(String[] args) {
        //创建TreeMap集合对象
        TreeMap<Student, String> map = new TreeMap<>();

        //创建学生对象
        Student s1 = new Student("张一", 66);
        Student s2 = new Student("张二", 35);
        Student s3 = new Student("钱三", 25);
        Student s4 = new Student("丁四", 25);
        Student s5 = new Student("李五", 95);
        Student s6 = new Student("王六", 15);

        //把学生对象作为key，字符串作为value，添加进集合
        map.put(s1,"哈尔滨");
        map.put(s2,"佳木斯");
        map.put(s3,"北京");
        map.put(s4,"上海");
        map.put(s5,"广州");
        map.put(s6,"齐齐哈尔滨");

        //调用forEach方法，创建BiConsumer的实现类对象，重写accept方法，在方法内输出key和value
        //有一种遍历方式
        map.forEach(new BiConsumer<Student, String>() {
            @Override
            public void accept(Student key, String value) {
                System.out.println(key+"------>"+value);
            }
        });

    }
}
